/*
BALACING AROUNF THE AGE 55 DISCONTINUITY

Data 	: B1_age55data.dta.
Folder 	: Jonas/data/B1_age55data.dta
Date	: 2017-05-16

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	2019-10-28

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/B3_firststage.log"	, replace 
use 		 "$datapath/A3_age55data_WC.dta"


*Keeping date at notification 
keep  if eventtime_def==0


{ // Define program F-test
*===============================================================================
capture program drop covFtest
program define covFtest, rclass
	test 	 `1' 
	qui	 estadd scalar F1  = r(F), replace
	qui	 estadd scalar p1 = r(p), replace
end
}
*


{ // Global define
*===============================================================================
global covariets 	"annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 educ3"
global firmcovar 	"firm_avg_earnings firm_avg_age firm_share_female firm_share_higheduc firm_noemployed"	
global instrument	"treat"
global allcovar		" annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 educ3 firm_avg_earnings firm_avg_age firm_share_female firm_share_higheduc firm_noemployed"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*
{ // Normalize variables
********************************************************************************

foreach var of   varlist annual_ear_prenot_def firm_avg_earnings firm_noemployed  tenureatnot {
	sum `var' 	if treat==0
	replace `var' = (`var' / r(mean) ) - 1
}	


}
*

{ // McCrary graph
*===============================================================================
preserve
local lopt "lstyle(foreground) lpattern(dash) lcolor(black)"
	gen freq=1

	collapse (sum) freq, by(age_def)
	keep if inrange(age_def,52,58)
	*Frequency graph
	twoway  (scatter freq age_def, ms(o) mcolor(black)) /// 
		(lfit 	 freq age_def if age_def<55, lcolor(black)) /// 
		(lfit 	 freq age_def if age_def>55, lcolor(black)) /// 
	,xline(55, `lopt') ///
	graphregion(color(white)) xtitle("Age at notification") ///
	ytitle("Frequency") legend(off) name(a,replace) xlabel(52(1)58) xscale(r(52 58))
	graph export "$agegraphpath/Balance/McCrary_freq.pdf", replace
	
	
	*Density graph
	egen totalobs = total(freq)
	gen dens = freq / totalobs
	twoway 	(scatter dens age_def, ms(o) mcolor(black)) /// 
		(lfit 	 dens age_def if age_def<55, lcolor(black)) /// 
		(lfit 	 dens age_def if age_def>55, lcolor(black)) /// 
	,xline(55, `lopt') ///
	graphregion(color(white)) xtitle("Age at notification") ///
	ytitle("Density") legend(off) name(b,replace) xlabel(52(1)58) xscale(r(52 58))
	graph export "$agegraphpath/Balance/McCrary_dens.pdf", replace
restore
}
*
{ // McCrary regression
*===============================================================================
preserve
	gen freq=1
	collapse (sum) freq, by(runvar treat inter)
	reg freq treat inter runvar, cluster(runvar)
	
	egen totalobs = total(freq)
	gen dens = freq / totalobs
	
	reg dens treat inter runvar, cluster(runvar)
restore
}
*

{ // Overall balance
*===============================================================================
eststo clear

***Linear

qui  eststo: reg treat	 $covariets 			$linear	 , cluster(varselid)
		covFtest  	 " $covariets "
estadd 	local inter  " $ \checkmark $ " 	, replace
estadd 	local lin  " $ \checkmark $ " 	, replace
estadd 	local quad "  " 		, replace

	
qui eststo: reghdfe treat	 $covariets 	$linear	 , cluster(varselid) absorb(ym_def)
		covFtest  	 " $covariets "
estadd 	local inter  " $ \checkmark $ " 	, replace
estadd 	local fixedym " $ \checkmark $ ", replace
estadd 	local lin  " $ \checkmark $ " 	, replace
estadd 	local quad "  " 		, replace
	
 eststo: reghdfe treat	 $covariets  $firmcovar 		$linear	 , cluster(varselid) absorb(ym_def)
		covFtest  	 " $covariets $firmcovar "
estadd 	local inter  " $ \checkmark $ " 	, replace		
estadd 	local fixedym " $ \checkmark $ ", replace
estadd 	local lin  " $ \checkmark $ " 	, replace
estadd 	local quad "  " 		, replace		

qui eststo: reghdfe treat	 $covariets  	 	$linear	 , cluster(varselid) absorb( varselid)
		covFtest  	 " $covariets "
estadd 	local inter  " $ \checkmark $ " 	, replace		
estadd 	local fixedym "  ", replace
estadd 	local lin  " $ \checkmark $ " 	, replace
estadd 	local quad "  " 		, replace
estadd  local dispFE 	" $ \checkmark $"		, replace


***Quadratic
 
 eststo: reghdfe treat	 $covariets $firmcovar 		$quadratic	 , cluster(varselid) absorb(ym_def )
		covFtest  	 " $covariets $firmcovar  "
estadd 	local inter  " $ \checkmark $ " 	, replace		
estadd 	local fixedym " $ \checkmark $ " , replace
estadd 	local lin  "  " 		 , replace
estadd 	local quad " $ \checkmark $ " 	 , replace


esttab , se(4) b(4) star(* 0.1 ** 0.05 *** 0.01) keep($covariets ) /// 
order(	annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 ///
	educ3)   stats(fake lin quad inter fixedym F1 p1 r2_a N_clust N)

esttab using "$agetablepath/Balance/tab_balance.csv",replace  booktabs nodepvars nomtitles ///
keep($covariets ) /// 
order(	annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 ///
	educ3)   ///
indicate("\textit{Firm Characteristics}=firm_avg_earnings" , labels(" $ \checkmark $ ") ) ///
varlabels( annual_ear_prenot_def "Earnings  (t-1)"   ///
	  female "Female" immigrant "Immigrant" tenureatnot "Tenure" educ1 "Primary" ///
	  educ2 "High school" educ3 "College" ) ///
	   se(4) b(4) star(* 0.1 ** 0.05 *** 0.01) 
	   
	   stop
	
esttab using "$agetablepath/Balance/tab_balance.tex", replace  booktabs nodepvars nomtitles ///
keep($covariets ) /// 
order(	annual_ear_prenot_def female immigrant tenureatnot educ1 educ2 ///
	educ3)   ///
indicate("\textit{Firm Characteristics}=firm_avg_earnings" , labels(" $ \checkmark $ ") ) ///
varlabels( annual_ear_prenot_def "Earnings  (t-1)"   ///
	  female "Female" immigrant "Immigrant" tenureatnot "Tenure" educ1 "Primary" ///
	  educ2 "High school" educ3 "College" ) ///
	   se(4) b(4) star(* 0.1 ** 0.05 *** 0.01) ///
stats(fake lin quad inter fixedym dispFE F1 p1 r2_a N_clust N, fmt(%9.0g %9.0g %9.0g %9.0g %9.0g %9.0g  %9.2f %9.3f  %9.3f %9.0g %9.0fc) 	 ///
labels("\textit{Polynomial order}" "\quad 1st degree" "\quad 2nd degree" "Interacted w. threshold" "Month/Year FE" "Displacement FE" " $ F$-statistic" " $ p$-value" "$ R^2 $" "\# clusters" "\# observations")) 	 ///
refcat(educ1 "\textit{Highest attanied Education}" , nolabel) ///
title("\textsc{Overall balancing} \label{tab:overallbalance}" ) ///
nonotes
}
*	

{ // Bivirate Balance (fill in manually in table)
*===============================================================================

matrix B = J(11,4,.)
foreach var of varlist 		 $covariets  {
	local j =  `j' +1 
	qui reghdfe `var' treat	$linear , absorb(ym_def) cluster(varselid)
	matrix B[`j',1] = _b[treat]
	matrix B[`j',2] = _se[treat]
	local t = abs(_b[treat]/_se[treat])
	matrix B[`j',3] = 2*ttail(e(df_r),abs(`t'))
	matrix B[`j',4] = e(N)

}
*
matrix list B


matrix B = J(11,4,.)
local j=0
foreach var of varlist 		 $covariets  {
	local j =  `j' +1 
	qui reghdfe `var' treat	$quadratic , absorb(ym_def) cluster(varselid)
	matrix B[`j',1] = _b[treat]
	matrix B[`j',2] = _se[treat]
	local t = abs(_b[treat]/_se[treat])
	matrix B[`j',3] = 2*ttail(e(df_r),abs(`t'))
	matrix B[`j',4] = e(N)

}
*
matrix list B



}


log close
